package org.jeecg.modules.demo.zc.chargingPile.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.trader.entity.Trader;
import org.jeecg.modules.demo.zc.chargingPile.entity.ChargingPile;
import org.jeecg.modules.demo.zc.chargingPile.service.IChargingPileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 充电桩
 */
@Api(tags="ChargingPile")
@RestController
@RequestMapping("/zc/ChargingPile")
@Slf4j
public class ChargingPileController extends JeecgController<ChargingPile, IChargingPileService> {

    @Autowired
    private IChargingPileService chargingPileService;

    /**
     * 分页列表查询
     *
     * @param chargingPile
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value="chargingPile-分页列表查询", notes="chargingPile-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ChargingPile>> queryPageList(ChargingPile chargingPile,
                                               @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                               @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                               HttpServletRequest req) {
        QueryWrapper<ChargingPile> queryWrapper = QueryGenerator.initQueryWrapper(chargingPile, req.getParameterMap());
        Page<ChargingPile> page = new Page<ChargingPile>(pageNo, pageSize);
        IPage<ChargingPile> pageList = chargingPileService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *   添加
     *
     * @param chargingPile
     * @return
     */
    @AutoLog(value = "chargingPile-添加")
    @ApiOperation(value="chargingPile-添加", notes="chargingPile-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ChargingPile chargingPile) {
        chargingPileService.save(chargingPile);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param chargingPile
     * @return
     */
    @AutoLog(value = "chargingPile-编辑")
    @ApiOperation(value="chargingPile-编辑", notes="chargingPile-编辑")
    //@RequiresPermissions("trader:trader:edit")
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody ChargingPile chargingPile) {
        chargingPileService.updateById(chargingPile);
        return Result.OK("操作成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "trader-通过id删除")
    @ApiOperation(value="trader-通过id删除", notes="trader-通过id删除")
    //@RequiresPermissions("trader:trader:delete")
    @PostMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        chargingPileService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "trader-批量删除")
    @ApiOperation(value="trader-批量删除", notes="trader-批量删除")
    //@RequiresPermissions("trader:trader:deleteBatch")
    @PostMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.chargingPileService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "trader-通过id查询")
    @ApiOperation(value="trader-通过id查询", notes="trader-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ChargingPile> queryById(@RequestParam(name="id",required=true) String id) {
        ChargingPile chargingPile = chargingPileService.getById(id);
        if(chargingPile==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(chargingPile);
    }
}
